import { launchRedPacket3Times } from "@/store/useRedPacket";
import {
  CourseWatchEntity,
  InviteCheckError,
  OnlineCountResponse,
  RedPacketData,
  RewardData
} from "@/typings/course";
import { CourseRewardInfo, LiveRoom, RewardData2 } from "@/typings/live";
import { http } from "@/utils/http";
import { parseUserAgent } from "@/utils/wechat";
import { showToast } from "vant";

export const checkEntrance = (params: InviteCheckError) =>
  http.request<InviteCheckError>({
    url: "/live/entryBefore",
    method: "POST",
    data: params
  });

export const getRoomData = params =>
  http.request<LiveRoom>({
    url: "/live/entry",
    method: "POST",
    data: { ...params }
  });

export const getRoomWatchData = params =>
  http.request<CourseWatchEntity>({
    url: "/live/watch",
    method: "POST",
    data: { ...params }
  });

export const getRedPacketList = params =>
  http.request<RedPacketData[]>({
    url: "/live/reward-record",
    method: "POST",
    data: { ...params }
  });

export const signin = params =>
  http.request({
    url: "/live/sign",
    method: "POST",
    data: { ...params }
  });

export const reward = async params => {
  const data = await http.request<RewardData>({
    url: "/live/reward",
    method: "POST",
    data: { ...params }
  });
  if (data) {
    const {
      sn,
      packageInfo,
      message = "单据信息缺失，请稍后再试",
      bizCode
    } = data;
    if (!packageInfo || !sn) {
      showToast(message);
      return;
    }
    if (bizCode !== "SUCCESS") {
      showToast(message);
      return;
    }
    launchRedPacket3Times({
      packageInfo,
      sn,
      sendPullState,
      callback: () => {}
    });
  }
  return data;
};

export const sendPullState = (pullState: number, sn: string) => {
  const { os, device, browser, wechat, network } = parseUserAgent();
  // 通过weixinjsbridge获取基础库版本
  return http.request({
    url: "/live/reward/pull-state",
    method: "POST",
    data: {
      pullState,
      sn,
      sysVersion: JSON.stringify(os),
      browerVersion: JSON.stringify(browser),
      wechatVersion: JSON.stringify(wechat)
    }
  });
};

export const answer = params =>
  http.request({
    url: "/live/answer",
    method: "POST",
    data: { ...params }
  });

export const updateLiveOnline = params =>
  http.request<OnlineCountResponse>({
    url: "/live/online",
    method: "POST",
    data: { ...params }
  });

export const completeLive = params =>
  http.request({
    url: "/live/completed",
    method: "POST",
    data: { ...params }
  });

export const addComplain = params =>
  http.request({
    url: "/subject/complaints/save",
    method: "POST",
    data: { ...params }
  });

export const getCourseRewardInfo = params =>
  http.request<CourseRewardInfo>({
    url: "/pay/reward/reissue",
    method: "POST",
    data: { ...params },
    headers: { "No-Auth": true }
  });

export const receiveReward = params =>
  http.request({
    url: "/pay/reward/reissue-receive",
    method: "POST",
    data: { ...params },
    headers: { "No-Auth": true }
  });

export const getRewardSN = params =>
  http.request<RewardData2[]>({
    url: "/hongBao/reissue/findReissue",
    method: "POST",
    data: { ...params }
  });
